package com.woohua.demo.wechat;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/***
 * @title ChatBotSend
 * @description
 * @author baiji
 * @version 1.0.0
 * @create 2023/7/6 14:30
 **/
public class ChatBotSend {
    //企业微信群机器人的WebHook地址xxx
    public static String WEBHOOK_TOKEN = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=1e84d0ff-8244-48f7-917c-39e479b35d51";

    public static void main(String args[]) throws Exception {
        HttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost(WEBHOOK_TOKEN);
        httppost.addHeader("Content-Type", "application/json; charset=utf-8");
        //构建一个json格式字符串textMsg，其内容是接收方需要的参数和消息内容
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msgtype", "text");

        JSONObject text = new JSONObject();
        text.put("content", "测试，请忽略。你好，我是机器人");
        JSONArray mentioned_mobile_list = new JSONArray();
        mentioned_mobile_list.add("15827281653");
        text.put("mentioned_mobile_list", mentioned_mobile_list);
        jsonObject.put("text", text);
        String textMsg = jsonObject.toJSONString();

        System.out.println("发送内容：" + jsonObject.toJSONString());
        StringEntity se = new StringEntity(textMsg, "utf-8");
        httppost.setEntity(se);
        HttpResponse response = httpclient.execute(httppost);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            String result = EntityUtils.toString(response.getEntity(), "utf-8");
            System.out.println(result);
        }
    }
}
